﻿using System;
using Microsoft.Practices.ObjectBuilder;
using AP.LUNA.DataEntities;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using AP.LUNA.Commons;

namespace AP.LUNA.Shell.Views
{
    public partial class RegistrarAsistencia : Microsoft.Practices.CompositeWeb.Web.UI.UserControl, IRegistrarAsistenciaView
    {
        private RegistrarAsistenciaPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public RegistrarAsistenciaPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	


        public object ListAlumnos
        {
            set
            {
                this.gvAlumnos.DataSource = value;
                this.gvAlumnos.DataBind();
            }
        }

        public int IdAsignatura
        {
            get
            {
                return (int)ViewState["IdAsignatura"];
            }
            set
            {
                ViewState["IdAsignatura"] = value;
            }
        }

        protected void gvAsignaturas_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            int index = int.Parse(e.CommandArgument.ToString());
            this.IdAsignatura = int.Parse(gvAsignaturas.DataKeys[index].Value.ToString());
            switch (e.CommandName)
            {
                case "Asistencia":
                    this.Presenter.RegistrarAsistencia();
                    break;
            }
        }


        public object ListAsignaturas
        {
            set
            {
                this.gvAsignaturas.DataSource = value;
                this.gvAsignaturas.DataBind();
            }
        }

        public int IndexView
        {
            set
            {
                this.MultiView1.ActiveViewIndex = value;
            }
        }
        protected void lkbVolver_Click(object sender, EventArgs e)
        {
            this.IndexView = 0;
        }
        protected void lkbGuardar_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvAlumnos.Rows)
            {
                asistencia a = new asistencia();
                a.IdAlumno = int.Parse(((System.Web.UI.WebControls.Label)row.FindControl("lbId")).Text);
                a.IdAsignatura = this.IdAsignatura;
                a.Fecha = this.Fecha;
                a.Id = Guid.NewGuid();
                a.Asistio = ((CheckBox)row.FindControl("chkAsiste")).Checked;
                _ListAsistencia.Add(a);
            }
            this.Presenter.Guardar();
        }

        private List<asistencia> _ListAsistencia = new List<asistencia>();
        public object ListAsistencia
        {
            get
            {
                return _ListAsistencia;
            }
            set
            {
                _ListAsistencia = (List<asistencia>)value;
            }
        }


        public string MsgResult
        {
            set { WebUtil.MostrarMensaje(Constanst.TITULO, value); }
        }
        protected void txtFecha_TextChanged(object sender, EventArgs e)
        {
            this.Presenter.CargarAlumnos();
        }


        public DateTime Fecha
        {
            get { return DateTime.Parse(this.txtFecha.Text); }
            set { this.txtFecha.Text = value.ToString("yyyy-MM-dd"); }
        }
    }
}

